<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    PHP 画图形 N行N列 |
    
    无敌小笼包</title>
  
    <link rel="shortcut icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
  
  <script src="/js/pace.min.js"></script>
</head>

<body>
<main class="content">
  <section class="outer">
  <article id="post-php画图形" class="article article-type-post" itemscope itemprop="blogPost">

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      PHP 画图形 N行N列
    </h1>
  

      </header>
    

    
      <div class="article-meta">
        <a href="/2016/05/05/php画图形/" class="article-date">
  <time datetime="2016-05-05T01:20:12.000Z" itemprop="datePublished">2016-05-05</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/Skill/">Skill</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      
      
      
        <p>写一个函数需要一个参数，根据这个参数输出一个图形<br><a id="more"></a></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="comment">/* 算法二、写一个函数需要一个参数，根据这个参数输出一个图形，</span></div><div class="line">比如：输入4：</div><div class="line"> 4     5       6     7</div><div class="line"> 15    16      17    8</div><div class="line"> 14    19      18    9</div><div class="line"> 13    12      11    10</div><div class="line"></div><div class="line">比如：输入3：</div><div class="line">3     4      5</div><div class="line">10    11     6</div><div class="line">9     8      7</div><div class="line">*/</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line">3*3+3-1   --&gt; 11</div><div class="line">4*4+4-1   --&gt; 19</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">$arr = [</div><div class="line">    [0,0,0],</div><div class="line">    [0,0,0],</div><div class="line">    [0,0,0],</div><div class="line">];</div><div class="line">     x  y</div><div class="line">$arr[0][0] = 3;</div><div class="line">$arr[0][1] = 4;</div><div class="line"></div><div class="line">向右移动y++</div><div class="line">向左移动y--</div><div class="line">左下移动x++</div><div class="line"></div><div class="line">移动的条件：</div><div class="line">    1. y++ &lt; $num 没出框</div><div class="line">    2. $arr[$x][$y+1] == 0 【没被占用】</div><div class="line"></div><div class="line">$arr = [</div><div class="line">    [3,4,5],</div><div class="line">    [10,11,6],</div><div class="line">    [9,8,7],</div><div class="line">];</div><div class="line">*/</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">jz</span><span class="params">($num)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="comment">/************************* 先构造二维数组 ********************/</span></div><div class="line">    <span class="keyword">for</span>($i=<span class="number">0</span>; $i&lt;$num; $i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">for</span>($j=<span class="number">0</span>; $j&lt;$num; $j++)</div><div class="line">        &#123;</div><div class="line">            $map[$i][$j] = <span class="number">0</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">/************************* 循环往数组中放 ********************/</span></div><div class="line">    <span class="comment">// 计算最后一个数</span></div><div class="line">    $end = $num * $num + $num - <span class="number">1</span>;</div><div class="line">    <span class="comment">// 初始化要放的位置</span></div><div class="line">    $x = $y = <span class="number">0</span>;</div><div class="line">    <span class="comment">// 初始化方向  // r:右 l：左 t：上 b：下</span></div><div class="line">    $d = <span class="string">'r'</span>;  </div><div class="line">    <span class="comment">// 先把第一个数放进去</span></div><div class="line">    $map[$x][$y] = $num;</div><div class="line">    <span class="comment">// 循环放从第二个数开始放</span></div><div class="line">    <span class="keyword">for</span>($i=$num+<span class="number">1</span>; $i&lt;=$end; $i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>($d == <span class="string">'r'</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>($y+<span class="number">1</span> &lt; $num &amp;&amp; $map[$x][$y+<span class="number">1</span>] == <span class="number">0</span>)</div><div class="line">                $y++;  <span class="comment">// 修改坐标</span></div><div class="line">            <span class="keyword">else</span></div><div class="line">                $d = <span class="string">'b'</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span>($d == <span class="string">'b'</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>($x+<span class="number">1</span> &lt; $num &amp;&amp; $map[$x+<span class="number">1</span>][$y] == <span class="number">0</span>)</div><div class="line">                $x++;  <span class="comment">// 修改坐标</span></div><div class="line">            <span class="keyword">else</span></div><div class="line">                $d = <span class="string">'l'</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span>($d == <span class="string">'l'</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>($y<span class="number">-1</span> &gt;= <span class="number">0</span> &amp;&amp; $map[$x][$y<span class="number">-1</span>] == <span class="number">0</span>)</div><div class="line">                $y--;  <span class="comment">// 修改坐标</span></div><div class="line">            <span class="keyword">else</span></div><div class="line">                $d = <span class="string">'t'</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span>($d == <span class="string">'t'</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>($x<span class="number">-1</span> &gt;= <span class="number">0</span> &amp;&amp; $map[$x<span class="number">-1</span>][$y] == <span class="number">0</span>)</div><div class="line">                $x--;  <span class="comment">// 修改坐标</span></div><div class="line">            <span class="keyword">else</span></div><div class="line">            &#123;</div><div class="line">                $d = <span class="string">'r'</span>;</div><div class="line">                $y++;  <span class="comment">// 修改坐标</span></div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="comment">// 把数进来</span></div><div class="line">        $map[$x][$y] = $i;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">/***************************** 画出图形 **************************/</span></div><div class="line">    $table = <span class="string">'&lt;table border="1"&gt;'</span>;</div><div class="line">    <span class="keyword">for</span>($i=<span class="number">0</span>; $i&lt;$num; $i++)</div><div class="line">    &#123;</div><div class="line">        $table .= <span class="string">'&lt;tr&gt;'</span>;</div><div class="line">        <span class="keyword">for</span>($j=<span class="number">0</span>; $j&lt;$num; $j++)</div><div class="line">        &#123;</div><div class="line">            $table .= <span class="string">'&lt;td&gt;'</span>;</div><div class="line">            $table .= $map[$i][$j];</div><div class="line">            $table .= <span class="string">'&lt;/td&gt;'</span>;</div><div class="line">        &#125;</div><div class="line">        $table .= <span class="string">'&lt;/tr&gt;'</span>;</div><div class="line">    &#125;</div><div class="line">    $table .= <span class="string">'&lt;/table&gt;'</span>;</div><div class="line">    <span class="keyword">echo</span> $table;</div><div class="line">&#125;</div><div class="line">jz(<span class="number">20</span>);</div><div class="line"></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://blog.mango16.cc/2016/05/05/php画图形/" data-id="cjrbtw1yo001u6d8hsymow9mz"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/算法/">算法</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2016/05/07/php工厂模式/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            PHP plant
          
        </div>
      </a>
    
    
      <a href="/2016/02/27/OOCSS/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">面向对象的CSS样式</div>
      </a>
    
  </nav>


  

  
    
  <div class="gitalk" id="gitalk-container"></div>
  <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
  <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
  <script type="text/javascript">
      var gitalk = new Gitalk({
        clientID: 'caba8826a61790936307',
        clientSecret: '09a15037dcab30d9adf35a53ec6961939169f797',
        repo: 'm9rco.github.io',
        owner: 'm9rco',
        admin: ['m9rco'],
        // id: location.pathname,      // Ensure uniqueness and length less than 50
        id: md5(location.pathname),
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

  gitalk.render('gitalk-container')
  </script>

  

</article>



</section>
  <footer class="footer">
  
  <div class="outer">
    <ul class="list-inline">
      <li>&copy; 2019 无敌小笼包</li>
      <i class="pulse"></i>
    </ul>
  </div>
</footer>

</main>
<aside class="sidebar">
  <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/hexo.svg" alt="无敌小笼包"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">相册</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/nagging">叽咕</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="Search">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
</aside>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

</body>
</html>